package com.cuotigou.controller;

import com.cuotigou.config.ConfigUtils;
import com.cuotigou.mapper.SMethodMapper;
import com.cuotigou.pojo.SKnowledge;
import com.cuotigou.pojo.SMethod;
import com.cuotigou.service.MethodService;
import com.cuotigou.util.ObjectResult;
import com.cuotigou.util.StringUtil;
import com.cuotigou.util.XMLBean;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2018/11/8.
 */
@Controller
@RequestMapping("/method")
public class
MethodController {

    @Autowired
    private MethodService methodService;
    @Autowired
    private SMethodMapper sMethodMapper;

    /**
     * 学习方法
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public ObjectResult index(XMLBean xmlBean){
        if(StringUtil.isEmpty(xmlBean.getToken())){
            return ObjectResult.build("0","未登录");
        }
        return  methodService.methodIndex(xmlBean);

    }

    /**
     * 点赞
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/collectionAndFabulous", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public ObjectResult collectionAndFabulous(XMLBean xmlBean, HttpServletResponse response){
        if(StringUtil.isEmpty(xmlBean.getToken())){
            return ObjectResult.build("0","未登录");
        }
//        if(!StringUtil.isEmpty(xmlBean.getIsCollection())&&!StringUtil.isEmpty(xmlBean.getIsFabulous())){
//            return ObjectResult.build("0","参数传递有误");
//        }

        response.setHeader("Access-Control-Allow-Origin", "*");

        return  methodService.collectionAndFabulous(xmlBean);

    }

    /**
     * 我的收藏
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/collection", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public ObjectResult collection(XMLBean xmlBean) {
        if (StringUtil.isEmpty(xmlBean.getToken())) {
            return ObjectResult.build("0", "未登录");
        }

        return methodService.collection(xmlBean);
    }

    /**
     * 学习方法详情
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/details")
    public ModelAndView detals(XMLBean xmlBean, HttpServletResponse response) {
//        response.setHeader("Access-Control-Allow-Origin", "*");
        ModelAndView modelAndView = new ModelAndView("detail");

        SMethod sMethod = methodService.methodDetails(xmlBean);
        modelAndView.addObject("id",xmlBean.getId());
        modelAndView.addObject("token",xmlBean.getToken());
        modelAndView.addObject("isFabulous",sMethod.getIsFabulous());
        modelAndView.addObject("isCollection",sMethod.getIsCollection());
        modelAndView.addObject("fabulous",sMethod.getFabulous());
        modelAndView.addObject("collection",sMethod.getCollection());
        modelAndView.addObject("title",sMethod.getTitle());
        modelAndView.addObject("time",sMethod.getTime());
        modelAndView.addObject("content",sMethod.getContent());
        return modelAndView;
    }

    /**
     * 学习方法推举
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/recommend", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public ObjectResult recommend(XMLBean xmlBean, HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin", "*");
        return  methodService.methodRecommend(xmlBean);

    }

    /**
     * 学习方法分享
     * @param xmlBean
     * @return
     */
    @RequestMapping("/share")
    @ResponseBody
    public ObjectResult answer(XMLBean xmlBean){

        SMethod sMethod = sMethodMapper.selectByPrimaryKey(Long.parseLong(xmlBean.getId()));
        Map<String,Object> data = new HashMap<String,Object>();
        data.put("title",sMethod.getTitle());
        data.put("content",StringUtil.getTextFromTHML(StringEscapeUtils.unescapeHtml4(sMethod.getContent())));
        data.put("url", ConfigUtils.SYS_URL+"method/details?id="+xmlBean.getId()+"&token="+xmlBean.getToken());
        data.put("examine",ConfigUtils.EXAMINE);

        return ObjectResult.build("1","成功",data);
    }
}
